import superagent from 'superagent';
/* global host */

function Request({
  url,
  accept,
  contenttype,
  data,
  success,
  fail,
  error,
  type,
}) {
  const typeRes = this.getType(type);
  const urlRes = this.getUrl(host, url);
  this.url = urlRes;
  const request = new superagent.Request(typeRes, urlRes);
  this.request = request;
  if (contenttype && contenttype === 'form') {
    request.set('Content-Type', 'application/x-www-form-urlencoded');
  } else if (contenttype && contenttype === 'multipart') {
    request.set('Content-Type', 'multipart/form-data');
  }
  request.withCredentials();
  if (accept === 'text') {
    request.set('Accept', 'text/plain');
  } else {
    request.set('Accept', 'application/json');
  }
  if (type === 'GET') {
    request.query(data);
  } else {
    request.send(data);
  }
  this.execute({
    success,
    fail,
    error,
    data,
  });
}

Request.prototype = {
  constructor: Request,
  getUrl: (host, url) => {
    if (/^http(s)?/.test(url)) {
      console.log('url1====', url);
      return url;
    }
    if (host && url[0] === '/') {
      console.log('url2=====', `${host}${url}`);
      return `${host}${url}`;
    }
  },
  getType: (type) => {
    const newType = type && type.toUpperCase();
    // return newType === 'POST' ? 'POST' : 'GET';
    return newType;
  },
  execute({
    success,
    fail,
    error,
  }) {
    this.startTime = Date.now();
    this.request.end((err, res) => {
      if (err) {
        console.log('request err:', err);
        // NetInfo.isConnected.fetch().done((isConnected) => {
        //   if (!isConnected) {
        //     if (Platform.OS === 'android') {
        //       ToastAndroid.show('网络好像有问题', ToastAndroid.SHORT);
        //     }
        //   }
        // });
        return error && error();
      }
      // log.saveAPI({
      //   url: this.url,
      //   time: Date.now() - this.startTime,
      // });
      // const body = res.body;
      // const errcode = body.errcode;
      // if (errcode !== 0) {
      //   log.saveBuss({
      //     errcode,
      //     errmsg: body.errmsg,
      //     url: this.url,
      //   });
      //   return fail && fail(body);
      // }
      if (parseInt(res.status, 10) < 300) {
        // console.log("rrrrrrrrrr: ", res);
        // if (res.body) {
        //   return success && success(res.body);
        // }
        return success && success(res.body);
      }
      return fail && fail(res.body);
    });
  },
};

const Index = {
  get: (options) => {
    const newOptions = options;
    newOptions.type = 'GET';
    return new Request(newOptions);
  },
  post: (options) => {
    const newOptions = options;
    newOptions.type = 'POST';
    return new Request(newOptions);
  },
  put: (options) => {
    const newOptions = options;
    newOptions.type = 'PUT';
    return new Request(newOptions);
  },
  delete: (options) => {
    const newOptions = options;
    newOptions.type = 'DELETE';
    return new Request(newOptions);
  },
  ajax: options => new Request(options),
};

export default Index;
